package com.phaeton.beemeup.entity;

import java.util.UUID;

import org.andengine.engine.handler.IUpdateHandler;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.util.Log;

import com.phaeton.beemeup.GameManager;

public class WaspComb extends Comb implements IComb {
	
	public WaspComb(final int col, final int row,
			ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager, final GameManager pGameManager) {
		super(col, row, pTiledTextureRegion, pGameManager);
		
		this.registerUpdateHandler(new IUpdateHandler() {

			@Override
			public void onUpdate(float pSecondsElapsed) {
				int surroundingCombs = 0;
				
				if(row > 0 && pGameManager.getLevel().getCell(col, row - 1).getState() != Cell.FREE)
					surroundingCombs++;
				
				if(row < pGameManager.getLevel().getDimension().getRow() - 1 && pGameManager.getLevel().getCell(col, row + 1).getState() != Cell.FREE) 
					surroundingCombs++;
				
				if(col > 0 && pGameManager.getLevel().getCell(col-1, row).getState() != Cell.FREE) 
					surroundingCombs++;
				
				if(col < pGameManager.getLevel().getDimension().getColumn() - 1 && pGameManager.getLevel().getCell(col+1, row).getState() != Cell.FREE) 
					surroundingCombs++;
				
				if(col % 2 == 0){
					if(col > 0 && row > 0 && pGameManager.getLevel().getCell(col - 1, row - 1).getState() != Cell.FREE)
						surroundingCombs++;
					
					if(col < pGameManager.getLevel().getDimension().getColumn() - 1 && row > 0 && pGameManager.getLevel().getCell(col+1, row - 1).getState() != Cell.FREE) 
						surroundingCombs++;
					if(row == 0 || row == pGameManager.getLevel().getDimension().getRow() -1)
						surroundingCombs += 3;
				}else{
					if(col > 0 && row < pGameManager.getLevel().getDimension().getRow() - 1 && pGameManager.getLevel().getCell(col - 1, row + 1).getState() != Cell.FREE)
						surroundingCombs++;
					
					if(col < pGameManager.getLevel().getDimension().getColumn() - 1 && row < pGameManager.getLevel().getDimension().getRow() - 1 
							&& pGameManager.getLevel().getCell(col+1, row + 1).getState() != Cell.FREE) 
						surroundingCombs++;
					if(row == 0 || row == pGameManager.getLevel().getDimension().getRow() -1)
						surroundingCombs ++;
				}
				
				if(col == 0 || col == pGameManager.getLevel().getDimension().getColumn() -1) {
					surroundingCombs += 2;
				}
				
				if(surroundingCombs >= 6 && (state & IComb.SURROUNDED) == 0)
					addState(IComb.SURROUNDED);
				else
					removeState(IComb.SURROUNDED);
				
				if(getMaterial() >= 5 && (state & SURROUNDED) == 0) {
					pGameManager.spawnWaspComb(WaspComb.this);
				}
			}

			@Override
			public void reset() {
			}
			
		});
	}

	public int getMaterial() {
		return super.getResource();
	}

	public void setStructure(int structure) {
		super.setStructure(structure);
	}

	public void changeStructure(int structure) {
		super.changeStructure(structure);
	}
	
	public void addMaterial(int i) {
		super.changeResource(i);
	}

	public void setMaterial(int mat) {
		super.setResource(mat);
	}

	public void changeMaterial(int mat) {
		super.changeResource(mat);
	}
	
	public int getStructure() {
		return structure;
	}
	
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if(pSceneTouchEvent.isActionDown() && (this.getState() & (IComb.OCCUPIED | IComb.FIGHT)) == 0){
			if(mGameManager.getSelectedBee() != null){
				Bee bee = mGameManager.getBees().get(mGameManager.getSelectedBee());
				bee.setTarget(this);
				bee.fly();
				return true;
			}
		}
		return false;
	}

	@Override
	public void damageComb(final UUID combID, final Insect attacker) {
		//auf 5 aendern
		Log.e("WaspComb", "damage started");
		this.registerUpdateHandler(new TimerHandler(2, true, new ITimerCallback() {
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				Log.e("WaspComb", "Structure: " + structure + " und Honey: " + getMaterial());
				if(getMaterial() > 0) {
					changeMaterial(-1);
				}
				else {
					if(getStructure() > 0) {
						//damageHandler.setTimerSeconds(10);
						changeStructure(-1);
					}
					else {
						WaspComb.this.unregisterUpdateHandler(pTimerHandler);
						attacker.removeState(Insect.FIGHT);
						mGameManager.destroyWaspComb(combID);
						//pTimerHandler.reset();
					}
				}
			}			
		}));
	}
}
